/**
 * 
 */
package com.geekteam.manga.ui.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.geekteam.manga.R;
import com.geekteam.manga.Utils.Constant;
import com.geekteam.manga.ui.adapter.TabPagerAdapter;
import com.geekteam.manga.wiget.TabPageIndicator;

/**
 * @author MtRol
 *
 */
public class MainTabFragment extends SherlockFragment{
	private TabPageIndicator mPagerIndicator;
	private ViewPager mPager;
	private int mSelectedPosition;
	private List<Fragment> mListFragment;
	
	public MainTabFragment(){
		
	}
	
	public MainTabFragment(int position, List<Fragment> listFragment) {
		this.mSelectedPosition = position;
		this.mListFragment = listFragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_main_tab, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		TabPagerAdapter adapter = new TabPagerAdapter(getChildFragmentManager(), mListFragment);
		
		mPager = (ViewPager) getActivity().findViewById(R.id.pager);
		mPager.setAdapter(adapter);
		mPager.setOffscreenPageLimit(4);
		
		mPagerIndicator = (TabPageIndicator) getActivity().findViewById(R.id.indicator);
		mPagerIndicator.setViewPager(mPager, mSelectedPosition);
		
		mPagerIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}

			@Override
			public void onPageSelected(final int position) {
				getActivity().runOnUiThread(new Runnable() {

					@Override
					public void run() {
						Intent intent = new Intent(
								Constant.FILTER_UPDATE_PAGE);
						intent.putExtra(Constant.PAGE_POSITION, position);
						getActivity().sendBroadcast(intent);
					}
				});
			}
			
		});
	}
}
